package com.zimmur.app.mocolink.manage.web.fdfs;

import java.io.IOException;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.zimmur.app.mocolink.domain.util.DateUtils;

/**
 * Fdfs客户端操作类
 * @author Administrator
 *
 */
public class FdfsClientUtil {
	private static Logger logger=LoggerFactory.getLogger(FdfsClientUtil.class);
	
	private static String AUTHOR = "author";	
	private static String FILE_NAME = "fileName";
	
	private static TrackerClient trackerClient;
	
	public static void setTrackerClient(TrackerClient trackerClient) {
		FdfsClientUtil.trackerClient = trackerClient;
	}
	/**
	 * 保存文件到FDFS
	 * @param fastDFSFile
	 * @param userId 当前用户ID或名字
	 * @return
	 */
	public static StorageFiles saveFileToFdfs(FastDFSFile fastDFSFile,String userId){
		StorageFiles storageFile = null;
		try {
			if (fastDFSFile.getSize() < 0 ) {
				return null;
			}
			TrackerServer trackerServer = null;
			StorageClient storageClient;
			StorageServer storageServer = null;
			try {
				 trackerServer = trackerClient.getConnection();
				 storageClient = new StorageClient(trackerServer, storageServer);
				 
				if(trackerServer==null || storageClient==null){
					logger.error("TrackerServer can not connect!");
					return null;
				}
				
				if (!StringUtils.isEmpty(fastDFSFile.getExt())) {
					NameValuePair nvp[] = new NameValuePair[] { new NameValuePair(AUTHOR, userId),
							new NameValuePair(FILE_NAME, fastDFSFile.getName()) };
					
					String[] uploadResults = storageClient.upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), nvp);
					String groupName = uploadResults[0];
					String remoteFileName = uploadResults[1];						
					
					if(!groupName.equals("") && !remoteFileName.equals("")){
						storageFile = new StorageFiles();
						
						storageFile.setFiletype("goods");
						storageFile.setOriginalfilename(fastDFSFile.getName());
						storageFile.setFilesize(fastDFSFile.getSize());
						storageFile.setAuthor(userId);
						storageFile.setFileext(fastDFSFile.getExt());
						storageFile.setStoragegroup(groupName);
						storageFile.setStorageid(remoteFileName);
						storageFile.setCreatedate(DateUtils.getNowUnixTime());
					}
				}
			} catch (IOException e) {
				logger.error(e.getMessage());
			}finally{				
				if(trackerServer != null)
					trackerServer.close();
			}
			
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return storageFile;
	}
	/**
	 * 删除Fastdfs文件
	 * @param groupName Fastdfs组名
	 * @param remote_filename Fastdfs文件名
	 * @return true 表示删除成功
	 */
	public static boolean deleteFdfsFile(String group_name,String remote_filename){
		try {
			TrackerServer trackerServer = null;
			StorageClient storageClient;
			StorageServer storageServer = null;
			try {
				trackerServer = trackerClient.getConnection();
				 storageClient = new StorageClient(trackerServer, storageServer);
				 
				if(trackerServer==null || storageClient==null){
					logger.error("TrackerServer can not connect!");
					return false;
				}
				//return 0 for success, none zero for fail (error code)
				int iRet=storageClient.delete_file(group_name, remote_filename);
				if(iRet==0){
					return true;
				}
				
			}catch (IOException e) {
				logger.error(e.getMessage());
			}finally{				
				if(trackerServer != null)
					trackerServer.close();
			}
		}catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return false;
	}
}
